#define LG_CORE

#include "lg_vertex.h"
#include <memory.h>

static struct {
	LGConfig* config;
	LGVertexBackend* backend;
}ctx = { 0 };

static void
freeVertex(LGRef* ref) {
	ctx.backend->freeVertex((LGVertex*)ref);
}


LG_API int
lgInitVertexContext(LGConfig* config, LGVertexBackend* backend) {
	ctx.config = config;
	ctx.backend = backend;
	return 0;
}

LG_API LGVertex*
lgNewVertex(LGVertexAttr* attrs,
	unsigned attrCount, unsigned eleSize, unsigned cap){
	size_t size = ctx.backend->sizeofVertex();
	LGVertex* vertex = lgNewRef(size, freeVertex);
	memcpy(vertex->attrs, attrs, sizeof(LGVertexAttr) * attrCount);
	vertex->attrCount = attrCount;
	vertex->eleSize = eleSize;
	vertex->totalData = 0;
	vertex->totalIndexs = 0;
	ctx.backend->newVertex(vertex, cap);
	return vertex;
}

LG_API void
lgClearVertex(LGVertex* vertex) {
	vertex->totalData = 0;
	vertex->totalIndexs = 0;
	ctx.backend->clearVertex(vertex);

}

LG_API void
lgClearVertexCap(LGVertex* vertex) {
	vertex->totalData = 0;
	vertex->totalIndexs = 0;
	ctx.backend->clearVertexCap(vertex);
	
}

LG_API void
lgPushVertexData(LGVertex* vertex, void* p, unsigned count) {
	ctx.backend->pushData(vertex, (char*)p, count);
	vertex->totalData += count;
}

LG_API void
lgPushVertexIndexs(LGVertex* vertex, unsigned* p, unsigned count) {
	ctx.backend->pushIndexs(vertex, p, count);
	vertex->totalIndexs += count;
}

LG_API void
lgUploadVertex(LGVertex* vertex) {
	if (!vertex->totalData) {
		return;
	}
	ctx.backend->uploadVertex(vertex);
	vertex->totalData = vertex->totalIndexs = 0;
}

LG_API unsigned
lgGetVetexTotalData(LGVertex* vertex) {
	return vertex->totalData;
}

LG_API unsigned
lgGetVetexTotalIndex(LGVertex* vertex) {
	return vertex->totalIndexs;
}

